package cn.factorybox.item.controller;

import cn.factorybox.common.entity.Result;
import cn.factorybox.item.annotation.ControllerLogAnnotation;
import cn.factorybox.item.service.FactoryDeviceDataService;
import cn.factorybox.model.entity.FactoryDevice;
import cn.factorybox.model.entity.FactoryJudgeValue;
import cn.factorybox.model.entity.FactoryDeviceData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-09-12 18:39
 */

@Api(tags = "工厂终端设备数据管理")
@Slf4j
@RestController
@RequestMapping("factorydata")
public class FactoryDeviceDataController {

    @Autowired
    private FactoryDeviceDataService factoryDeviceDataService;

    @ControllerLogAnnotation(moduleName = "终端设备数据管理", functionName = "接收工厂的json数据流")
    @ApiOperation(value = "接收工厂的json数据流", notes = "接收工厂的json数据流")
    @PostMapping("saveFactory")
    public int save(@RequestBody FactoryDeviceData factoryDeviceData) {
        log.info("接收到上位机的数据：{}",factoryDeviceData.toString());
        //对数据进行判断，如果数据异常，则将某个数据值存入异常数据表中
        int count = factoryDeviceDataService.judgeData(factoryDeviceData);
        return factoryDeviceDataService.saveFactoryDeviceData(factoryDeviceData,count);
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "修改数据阈值")
    @ApiOperation(value = "修改数据阈值",notes = "修改数据阈值")
    @PostMapping("/threshold")
    public Result updateThreshold(@RequestBody FactoryJudgeValue factoryJudgeValue) {
        factoryDeviceDataService.updateThreshold(factoryJudgeValue);
        return Result.ok("修改数据阈值成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "获取数据阈值")
    @ApiOperation(value = "获取数据阈值",notes = "获取数据阈值")
    @GetMapping("/threshold")
    public Result selectThresholdList() {
        Map<String,Object> data = factoryDeviceDataService.getThresholdValue();
        return Result.ok(data,"获取数据阈值成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "获取最新数据")
    @ApiOperation(value = "获取最新数据", notes = "获取最新数据")
    @GetMapping("newestData")
    public Result getNewestData(@RequestParam("deviceID") String fSerialNumber) throws Exception {
        Map<String,Object> map = factoryDeviceDataService.getNewestData(fSerialNumber);
        return Result.ok(map,"获取最新数据成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "获取当日峰值")
    @ApiOperation(value = "获取当日峰值", notes = "获取当日峰值")
    @GetMapping("peak")
    public Result getCrestValue(@RequestParam("deviceID") String fSerialNumber) {
        Map<String,Object> map = factoryDeviceDataService.getPeak(fSerialNumber);
        return Result.ok(map,"成功获峰值数据");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "获取变化曲线数据")
    @ApiOperation(value = "获取变化曲线数据", notes = "获取变化曲线数据")
    @GetMapping("changeCurve")
    public Result getChangeCurveData(@RequestParam("deviceID") String fSerialNumber) {
        Map<String,Object> map = factoryDeviceDataService.getChangeCurveData(fSerialNumber);
        return Result.ok(map,"成功获峰值数据");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "获取满足率")
    @ApiOperation(value = "获取满足率", notes = "获取满足率")
    @GetMapping("satisfy")
    public Result getSatisfy(@RequestParam("deviceID") String fSerialNumber) {
        Map<String,Object> data = new HashMap<>();
        Map<String,Object> map = factoryDeviceDataService.getSatisfy(fSerialNumber);
        data.put("data",map);
        return Result.ok(data,"获取满足率成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "获取今日异常数据列表")
    @ApiOperation(value = "获取今日异常数据列表", notes = "获取今日异常数据列表")
    @GetMapping("abnormal")
    public Result getAbnormalDataList(@RequestParam("deviceID") String fSerialNumber) {
        Map<String,Object> map = factoryDeviceDataService.getAbnormalDataList(fSerialNumber);
        return Result.ok(map,"获取今日异常数据列表成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "删除数据")
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @DeleteMapping("delete")
    public Result deleteData(@RequestParam("id") String id) {
        factoryDeviceDataService.deleteData(id);
        return Result.ok("删除数据成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "修改数据阈值")
    @ApiOperation(value = "修改数据阈值", notes = "修改数据阈值")
    @PostMapping("editThreshold")
    public Result editThreshold(@RequestBody Map<String,Object> map) {
        factoryDeviceDataService.editThreshold(map);
        return Result.ok("修改数据阈值");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "查询某天某设备的数据列表")
    @ApiOperation(value = "查询某天某设备的数据列表", notes = "查询某天某设备的数据列表")
    @GetMapping("oneDevice")
    public Result selectOneDeviceData(@RequestParam Map<String,Object> map) {
        Map<String,Object> data = factoryDeviceDataService.selectOneDeviceData(map);
        return Result.ok(data,"获取数据列表成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "删除异常数据")
    @ApiOperation(value = "删除异常数据", notes = "删除异常数据")
    @DeleteMapping("deleteAbnormal")
    public Result deleteAbnormalData(@RequestParam("id") String id) {
        factoryDeviceDataService.deleteAbnormalData(id);
        return Result.ok("删除异常数据成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "查询异常数据信息")
    @ApiOperation(value = "查询异常数据信息", notes = "查询异常数据信息")
    @GetMapping("abnormalMessage")
    public Result getAbnormalMessageList(@RequestParam Map<String,Object> map) {
        Map<String,Object> data = factoryDeviceDataService.getAbnormalMessageList(map);
        return Result.ok(data,"获取异常数据列表成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "根据房间id和日期获取数据")
    @ApiOperation(value = "根据房间id和日期获取数据", notes = "根据房间id和日期获取数据")
    @GetMapping("/datalist")
    public Result selectDataList(@RequestParam("roomId") String roomId, @RequestParam("date") String date) {
        return new Result(factoryDeviceDataService.selectDataListByRoomIdAndDate(roomId,date),"数据获取成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "获取首页状态数据（所有设备的状态总览）")
    @ApiOperation(value = "获取首页状态数据（所有设备的状态总览）", notes = "获取首页状态数据（所有设备的状态总览）")
    @GetMapping("/home")
    public Result selectAllDeviceDataState(HttpServletRequest request) throws Exception {
        List<FactoryDevice> list = factoryDeviceDataService.selectAllDeviceDataStatue(request);
        return new Result(list,"获取首页状态数据成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备数据管理", functionName = "获取超管首页状态数据（所有设备的状态总览）")
    @ApiOperation(value = "获取超管首页状态数据（所有设备的状态总览）", notes = "获取超管首页状态数据（所有设备的状态总览）")
    @GetMapping("/managerhome")
    public Result selectManagerHomeState(HttpServletRequest request) throws Exception {
        return new Result(factoryDeviceDataService.selectManagerHomeState(request),"获取超管首页状态数据成功");
    }
}
